﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLayer;
using Library;

public partial class Administrator_MenuEdit : System.Web.UI.Page
{
    string cmd = "";
        int Id = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        cmd = Request.QueryString["cmd"].ToString();
        switch (cmd)
        {
            case "new":
                rbStatus.Enabled = false;
                txtOrder.Enabled = false;
                btnNew.Enabled = true;
                btnUpdate.Enabled = false;
                break;
            case "edit":
                txtOrder.Enabled = true;
                rbStatus.Enabled = true;
                btnNew.Enabled = false;
                btnUpdate.Enabled = true;
                Id = Convert.ToInt32(Request.QueryString["Id"].ToString());
                if (!IsPostBack)
                {
                    FillData(BusinessLayer.Menu.GetOne(Id));
                }
                break;
        }
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        BusinessLayer.Menu menu = new BusinessLayer.Menu();
        menu.Menu_Name = txtTen.Text;
        menu.Menu_Detail = txtMota.Text;
        menu.Menu_Order = BusinessLayer.Menu.GetOrder() + 1;
        menu.Parent_Menu_ID = Convert.ToInt32(DropDownList1.SelectedValue);
        try
        {
            BusinessLayer.Menu.Insert(menu);
            MessageBox.Show("Menu đã thêm thành công !");
            Response.Redirect("Menu.aspx");
        }
        catch (Exception ex)
        {
            MessageBox.Show("Lỗi !!! Mời thử lại");
            throw ex;
        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        BusinessLayer.Menu menu = new BusinessLayer.Menu();
        menu.Menu_ID = Id;
        menu.Menu_Name = txtTen.Text;
        menu.Menu_Detail = txtMota.Text;
        menu.Menu_Order = BusinessLayer.Menu.GetOrder() + 1;
        menu.Parent_Menu_ID = Convert.ToInt32(DropDownList1.SelectedValue);
        try
        {
            BusinessLayer.Menu.Update(menu);
            MessageBox.Show("Menu đã sửa thành công !");
            Response.Redirect("MenuEdit.aspx?cmd=edit&Id=" + Id.ToString());
        }
        catch (Exception ex)
        {
            MessageBox.Show("Lỗi !!! Mời thử lại");
            throw ex;
        }
    }
    protected void BbtnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("Menu.aspx");
    }
    protected void FillData(BusinessLayer.Menu menu)
    {
        txtTen.Text = menu.Menu_Name;
        txtMota.Text = menu.Menu_Detail;
        txtOrder.Text = menu.Menu_Order.ToString();
        rbStatus.Items.FindByValue(menu.Menu_Status.ToString()).Selected = true;
    }
}